package advanced.thread;

/**
 * 查看线程的状态
 * <p>
 * 线程的优先级 高低代表的是概率
 * 优先级越高不一定先执行!!!
 */
public class L8 {
    public static void main(String[] args) throws InterruptedException {

        Thread t = new Thread(() -> {
            for (int i = 0; i < 5; i++) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("...");
            }
        });
        Thread.State state = t.getState();
        System.out.println(state);//新生状态
        t.start();
        System.out.println(t.getState());//就绪状态
        while (state != Thread.State.TERMINATED) {
            int activeCount = Thread.activeCount();//活动的线程数
            System.out.println(activeCount);
            if (activeCount == 1) break;

            Thread.sleep(200);
            state = t.getState();
            System.out.println(state);//运行状态
        }

    }
}
